package com.cloud.sysmana.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.cloud.sysmana.dto.SysStore;
import com.cloud.sysmana.entity.SysRole;
import com.cloud.sysmana.entity.SysUser;
import com.cloud.sysmana.entity.SysUserRole;
import com.cloud.sysmana.ser.ISysDepaService;
import com.cloud.sysmana.ser.ISysRoleService;
import com.cloud.sysmana.ser.ISysUserService;
import com.cloud.util.base.BaseAction;
import com.cloud.util.base.IUserInfo;
import com.cloud.util.base.RetType;
import com.cloud.util.other.MD5;
import com.cloud.util.page.PageInfo;

/**
 * 系统用户管理
 * @author ytw
 *
 */
@Controller
@RequestMapping("system")
public class SysUserAction extends BaseAction{
	
	@Resource(name="sysUserService")
	private ISysUserService sysUserService;
	
	@Resource(name="sysRoleService")
	private ISysRoleService sysRoleService;	
	@Resource(name="sysDepaService")
	private ISysDepaService sysDepaService;
	
	/**
	 * 系统用户列表
	 * @return
	 */
	@RequestMapping("listSysUser.do")
	public String listSysUser(ModelMap modelMap, PageInfo pageInfo, SysStore dataStore){
		
		PageInfo retPageInfo = sysUserService.listUserPage(pageInfo,dataStore);
		modelMap.put("pageInfo", retPageInfo);
		modelMap.put("dataStore", dataStore);
		
		return getViewsModelPathSys("sysmana/user/userList");
	}
	
	/**
	 * 添加用户登录
	 * @return
	 */
	@RequestMapping("addUserLogin.do")
	public String addUserLogin(ModelMap modelMap){
		
		List<SysRole> roleList = sysRoleService.listAllRole();
		modelMap.put("roleList", roleList);
		modelMap.put("departs", sysDepaService.getDepaMap());
		return getViewsModelPathSys("sysmana/user/addUser");
	}
	/**
	 * 删除系统用户
	 * @param userId
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delSysUser.do")
	public RetType delSysUser(ModelMap modelMap,String ids){
		
		RetType reType = sysUserService.delSysUser(ids);
		
		return reType;
	}
	
	/**
	 * 编辑系统用户登录
	 * @param userId
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="editSysUserLogin.do")
	public String editSysUserLogin(ModelMap modelMap, int userId){
		//获取用户信息
		SysUser sysUser = sysUserService.getUserById(userId);
		modelMap.put("sysUser", sysUser);
		
		//获取当前用户对应的系统角色
		try {
			List role =sysRoleService.getRoleIdByUserId(userId);
			modelMap.put("roleId", role.get(0));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		//获取系统角色
		List<SysRole> roleList = sysRoleService.listAllRole();
		modelMap.put("roleList", roleList);
		
		//获取用户角色
		List<SysUserRole> userRoles = sysUserService.listUserRolesByUserId(userId);
		modelMap.put("userRoles", userRoles);
		
		modelMap.put("departs", sysDepaService.getDepaMap());
		return getViewsModelPathSys("sysmana/user/editUser");
	}
	
	/**
	 * 添加/编辑系统用户
	 * @param modelMap
	 * @param sysUser
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updateSysUser.do")
	public RetType updateSysUser(ModelMap modelMap, SysUser sysUser, String[] roles){
		sysUserService.updateSysUser(sysUser, roles);
		RetType reType = new RetType();
		reType.setErrCode(RetType.RET_SUCCESS);
		return reType;
	}
	
	/**
	 * 重置系统用户密码
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="resetPWD.do")
	public RetType resetPWD(int id){
		sysUserService.resetPWD(id);
		RetType reType = new RetType();
		reType.setErrCode(RetType.RET_SUCCESS);
		return reType;
	}
	
	/**
	 * 修改密码登录
	 * @return
	 */
	@RequestMapping(value="editPWDLogin.dol")
	public String editPWDLogin(){
		return getViewsModelPathSys("sysmana/user/editPWD");
	}
	
	/**
	 * 修改密码
	 * @param request
	 * @param oldPWD
	 * @param newPWD1
	 * @param newPWD2
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="editPWD.dol")
	public RetType editPWD(HttpServletRequest request,String oldPWD,String newPWD1,String newPWD2){
		IUserInfo userInfo = getUserInfo(request);
		RetType retType = new RetType();
		if (!userInfo.getPwd().equals(MD5.toMD5(oldPWD.getBytes()))) {
			retType.setErrCode(RetType.RET_FAILE);
			retType.setErrMsg("原密码错误！");
			return retType;
		}
		if (!newPWD1.equals(newPWD2)) {
			retType.setErrCode(RetType.RET_FAILE);
			retType.setErrMsg("两次输入密码不一致！");
			return retType;
		}
		sysUserService.editPWD(userInfo.getUserId(), newPWD2);
		retType.setErrCode(RetType.RET_SUCCESS);
		return retType;
	}
	
	/**
	 * 查询是否有上下级
	 * @param deparId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getDeparPid.dop")
	public RetType getDeparPid(HttpServletRequest request,Integer deparId){
		//IUserInfo userInfo = getUserInfo(request);
		RetType retType = new RetType();
		String code=sysDepaService.getPid(deparId);
//		if (code.equals("noFloor")) {
//			retType.setData(bgPoliceJobService.getNameListByFloor(0));
//			retType.setErrCode("1");
//			retType.setErrMsg("无下级");
//		}
//		else if (code.equals("oneFloor")) {
//			List<String> jobList=bgPoliceJobService.getNameListByFloor(1);
//			retType.setData(jobList);
//			retType.setErrCode("1");
//			retType.setErrMsg("有一层下级");
//		}
//		else if (code.equals("twoFloor")) {
//			retType.setData(bgPoliceJobService.getNameListByFloor(2));
//			retType.setErrCode("1");
//			retType.setErrMsg("有两层下级");
//		}
//		else if (code.equals("threeFloor")) {
//			retType.setData(bgPoliceJobService.getNameListByFloor(3));
//			retType.setErrCode("1");
//			retType.setErrMsg("有三层下级");
//		}
//		else{
//			retType.setErrCode("2");
//			retType.setErrMsg("发送错误！");
//		}
		return retType;
	}
	
	/**
	 * 添加用户前先验证该用户名是否存在
	 * @param deparId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="validateUserName.do")
	public RetType getUserName(HttpServletRequest request,String name){
		RetType retType = new RetType();
		String result=sysUserService.getUserNameIsExist(name);
		if (result.equals("exist")) {
			retType.setErrCode("2");
			retType.setErrMsg("该登录账号已存在！");
		}
		else{
			retType.setErrCode("1");
			retType.setErrMsg("该登录账号可用");
		}
		return retType;
	}
	
	/**
	 * 验证单位主管或者单位监管是否已经存在
	 * @param deparId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="validateCharge.do")
	public RetType validateCharge(HttpServletRequest request,String deparId,String userJobId){
		RetType retType = new RetType();
		String result=sysUserService.getChargeIsExist(deparId,userJobId);
		if (result.equals("exist"+userJobId)) {
			retType.setErrCode("2");
			retType.setErrMsg(userJobId+"已存在!");
		}
		else{
			retType.setErrCode("1");
			retType.setErrMsg("可以添加"+userJobId);
		}
		return retType;
	}

}
